home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / GraphicsCards / StormMesa / demos / texcyl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-15  |  5.3 KB  |  252 lines

  1. /* $Id: texcyl.c,v 3.1 1998/06/23 03:16:51 brianp Exp $ */
  2.  
  3. /*
  4.  * Textured cylinder demo: lighting, texturing, reflection mapping.
  5.  * Brian Paul  May 1997  This program is in the public domain.
  6.  */
  7.  
  8. /*
  9.  * $Log: texcyl.c,v $
  10.  * Revision 3.1  1998/06/23 03:16:51  brianp
  11.  * added Point/Linear sampling menu items
  12.  *
  13.  * Revision 3.0  1998/02/14 18:42:29  brianp
  14.  * initial rev
  15.  *
  16.  */
  17.  
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <math.h>
  22. #include <GL/glut.h>
  23.  
  24. #ifndef AMIGA
  25. #include "../util/readtex.c"   /* I know, this is a hack. */
  26. #else
  27. GLboolean LoadRGBMipmaps( const char *, GLint );
  28. #endif
  29.  
  30.  
  31. #define LIT 1
  32. #define TEXTURED 2
  33. #define REFLECT 3
  34. #define ANIMATE 10
  35. #define POINT_FILTER 20
  36. #define LINEAR_FILTER 21
  37. #define QUIT 100
  38.  
  39. static GLuint CylinderObj = 0;
  40. static GLboolean Animate = GL_TRUE;
  41.  
  42. static GLfloat Xrot = 0.0, Yrot = 0.0, Zrot = 0.0;
  43. static GLfloat DXrot = 1.0, DYrot = 2.5;
  44.  
  45.  
  46. static void Idle( void )
  47. {
  48.    if (Animate) {
  49.       Xrot += DXrot;
  50.       Yrot += DYrot;
  51.       glutPostRedisplay();
  52.    }
  53. }
  54.  
  55.  
  56. static void Display( void )
  57. {
  58.    glClear( GL_COLOR_BUFFER_BIT );
  59.  
  60.    glPushMatrix();
  61.    glRotatef(Xrot, 1.0, 0.0, 0.0);
  62.    glRotatef(Yrot, 0.0, 1.0, 0.0);
  63.    glRotatef(Zrot, 0.0, 0.0, 1.0);
  64.    glScalef(5.0, 5.0, 5.0);
  65.    glCallList(CylinderObj);
  66.  
  67.    glPopMatrix();
  68.  
  69.    glutSwapBuffers();
  70. }
  71.  
  72.  
  73. static void Reshape( int width, int height )
  74. {
  75.    glViewport( 0, 0, width, height );
  76.    glMatrixMode( GL_PROJECTION );
  77.    glLoadIdentity();
  78.    glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
  79.    glMatrixMode( GL_MODELVIEW );
  80.    glLoadIdentity();
  81.    glTranslatef( 0.0, 0.0, -70.0 );
  82. }
  83.  
  84.  
  85. static void SetMode(GLuint m)
  86. {
  87.    /* disable everything */
  88.    glDisable(GL_LIGHTING);
  89.    glDisable(GL_TEXTURE_2D);
  90.    glDisable(GL_TEXTURE_GEN_S);
  91.    glDisable(GL_TEXTURE_GEN_T);
  92.  
  93.    /* enable what's needed */
  94.    if (m==LIT) {
  95.       glEnable(GL_LIGHTING);
  96.    }
  97.    else if (m==TEXTURED) {
  98.       glEnable(GL_TEXTURE_2D);
  99.    }
  100.    else if (m==REFLECT) {
  101.       glEnable(GL_TEXTURE_2D);
  102.       glEnable(GL_TEXTURE_GEN_S);
  103.       glEnable(GL_TEXTURE_GEN_T);
  104.    }
  105. }
  106.  
  107.  
  108. static void ModeMenu(int entry)
  109. {
  110.    if (entry==ANIMATE) {
  111.       Animate = !Animate;
  112.    }
  113.    else if (entry==POINT_FILTER) {
  114.       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  115.       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  116.    }
  117.    else if (entry==LINEAR_FILTER) {
  118.       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  119.       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  120.    }
  121.    else if (entry==QUIT) {
  122.       exit(0);
  123.    }
  124.    else {
  125.       SetMode(entry);
  126.    }
  127.    glutPostRedisplay();
  128. }
  129.  
  130.  
  131. static void Key( unsigned char key, int x, int y )
  132. {
  133.    switch (key) {
  134.       case 27:
  135.      exit(0);
  136.      break;
  137.    }
  138.    glutPostRedisplay();
  139. }
  140.  
  141.  
  142. static void SpecialKey( int key, int x, int y )
  143. {
  144.    float step = 3.0;
  145.  
  146.    switch (key) {
  147.       case GLUT_KEY_UP:
  148.      Xrot += step;
  149.      break;
  150.       case GLUT_KEY_DOWN:
  151.      Xrot -= step;
  152.      break;
  153.       case GLUT_KEY_LEFT:
  154.      Yrot += step;
  155.      break;
  156.       case GLUT_KEY_RIGHT:
  157.      Yrot -= step;
  158.      break;
  159.    }
  160.    glutPostRedisplay();
  161. }
  162.  
  163.  
  164. static void Init( void )
  165. {
  166.    GLUquadricObj *q = gluNewQuadric();
  167.    CylinderObj = glGenLists(1);
  168.    glNewList(CylinderObj, GL_COMPILE);
  169.  
  170.    glTranslatef(0.0, 0.0, -1.0);
  171.  
  172.    /* cylinder */
  173.    gluQuadricNormals(q, GL_SMOOTH);
  174.    gluQuadricTexture(q, GL_TRUE);
  175.    gluCylinder(q, 0.6, 0.6, 2.0, 24, 1);
  176.  
  177.    /* end cap */
  178.    glTranslatef(0.0, 0.0, 2.0);
  179.    gluDisk(q, 0.0, 0.6, 24, 1);
  180.  
  181.    /* other end cap */
  182.    glTranslatef(0.0, 0.0, -2.0);
  183.    gluQuadricOrientation(q, GLU_INSIDE);
  184.    gluDisk(q, 0.0, 0.6, 24, 1);
  185.  
  186.    glEndList();
  187.    gluDeleteQuadric(q);
  188.  
  189.    /* lighting */
  190.    glEnable(GL_LIGHTING);
  191.    {
  192.       GLfloat gray[4] = {0.2, 0.2, 0.2, 1.0};
  193.       GLfloat white[4] = {1.0, 1.0, 1.0, 1.0};
  194.       GLfloat teal[4] = { 0.0, 1.0, 0.8, 1.0 };
  195.       glMaterialfv(GL_FRONT, GL_DIFFUSE, teal);
  196.       glLightfv(GL_LIGHT0, GL_AMBIENT, gray);
  197.       glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
  198.       glEnable(GL_LIGHT0);
  199.    }
  200.  
  201.    /* fitering = nearest, initially */
  202.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  203.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  204.  
  205.    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  206.    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  207.  
  208.    glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  209.    glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  210.  
  211.    if (!LoadRGBMipmaps("reflect.rgb", GL_RGB)) {
  212.       printf("Error: couldn't load texture image\n");
  213.       exit(1);
  214.    }
  215.  
  216.    glEnable(GL_CULL_FACE);  /* don't need Z testing for convex objects */
  217.  
  218.    SetMode(LIT);
  219. }
  220.  
  221.  
  222. int main( int argc, char *argv[] )
  223. {
  224.    glutInit( &argc, argv );
  225.    glutInitWindowSize( 400, 400 );
  226.  
  227.    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  228.  
  229.    glutCreateWindow(argv[0] );
  230.  
  231.    Init();
  232.  
  233.    glutReshapeFunc( Reshape );
  234.    glutKeyboardFunc( Key );
  235.    glutSpecialFunc( SpecialKey );
  236.    glutDisplayFunc( Display );
  237.    glutIdleFunc( Idle );
  238.  
  239.    glutCreateMenu(ModeMenu);
  240.    glutAddMenuEntry("Lit", LIT);
  241.    glutAddMenuEntry("Textured", TEXTURED);
  242.    glutAddMenuEntry("Reflect", REFLECT);
  243.    glutAddMenuEntry("Point Filtered", POINT_FILTER);
  244.    glutAddMenuEntry("Linear Filtered", LINEAR_FILTER);
  245.    glutAddMenuEntry("Toggle Animation", ANIMATE);
  246.    glutAddMenuEntry("Quit", QUIT);
  247.    glutAttachMenu(GLUT_RIGHT_BUTTON);
  248.  
  249.    glutMainLoop();
  250.    return 0;
  251. }
  252.